package com.example;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.config.Constants;
import com.example.util.MyFileUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParseHtml {
    public static final String CATE_FLAG = "、";
    public static final String WORD_FILE = Constants.DEFAULT_FOLDER_TMP + "/市场快讯（第三十期）-20210802.doc";

    public static void main(String[] args) throws Exception {
        String html = Word2Html.wordBytes2HtmlString(MyFileUtil.readBytes(WORD_FILE));
        //System.out.println(html);
        JSONObject data = new JSONObject();
        Document doc = Jsoup.parse(html);
        doc.outputSettings().prettyPrint(false);
        //获取目录
        Elements titles = doc.select("ol");
        Iterator<Element> iterator = titles.iterator();
        while (iterator.hasNext()) {
            Element title = iterator.next();
            //分类名称
            handleCategory(data, doc, title);
        }
        //System.out.println(data);
    }

    private static void handleCategory(JSONObject data, Document doc, Element title) {
        Element category = title.previousElementSibling();
        String categoryName = category.text();
        categoryName = categoryName.substring(categoryName.lastIndexOf(CATE_FLAG) + 1);
        JSONArray categoryItem = new JSONArray();
        data.put(categoryName, categoryItem);
        Elements children = title.children();
        Iterator<Element> childrenIterator = children.iterator();
        while (childrenIterator.hasNext()) {
            handleInfoItem(doc, categoryItem, childrenIterator);
        }
        if ("数据链接".equals(categoryName)){
            for (Object o : categoryItem) {
                JSONObject json = (JSONObject) o;
                System.out.println(json.getString("context"));
            }
        }
    }

    /**
     * 处理单个市场信息
     * @param doc
     * @param categoryItem
     * @param childrenIterator
     */
    private static void handleInfoItem(Document doc, JSONArray categoryItem, Iterator<Element> childrenIterator) {
        Element child = childrenIterator.next();
        String titleStr = child.text();
        JSONObject titleItem = new JSONObject();
        titleItem.put("title", titleStr);
        try {
            Elements a = child.getElementsByTag("a");
            //获取目录上的链接
            String link = a.first().attr("href").replace("#", "");
            //按标题,找正文段落
            handleContextNodeByTitle(doc, titleStr, titleItem,link);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        categoryItem.add(titleItem);
    }

    private static void handleContextNodeByLink(Document doc, JSONObject titleItem, String link) {
        titleItem.put("link", link);
        Elements info = doc.select("[name='" + link + "']");
        if (info == null){
            return;
        }
        Element contextNode = info.first().parent().nextElementSibling();
        handleContextNode(titleItem, contextNode);
    }

    private static void handleContextNodeByTitle(Document doc, String titleStr, JSONObject titleItem, String link) {
        Element info = doc.select("span:matchesOwn(" + titleStr + ")").last();
        //如果未找到,继续以链接查找
        if (info == null){
            handleContextNodeByLink(doc, titleItem, link);
            return;
        }
        while (info.parent()!=null && !"p".equals(info.parent().tagName())){
            info = info.parent();
        }
        Element contextNode = info.parent().nextElementSibling();
        handleContextNode(titleItem, contextNode);
    }

    public static final Pattern PATTERN = Pattern.compile("(?<=\\()[^\\)]+(?=\\))");
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    private static void handleContextNode(JSONObject titleItem, Element contextNode) {
        String text = contextNode.text();
        //获取时间
        Matcher matcher = PATTERN.matcher(text);
        if (matcher.find()) {
            String time = matcher.group(0);
            titleItem.put("time", DateUtil.format(DateUtil.parse(time),DATE_FORMAT));
        }
        String outerHtml = contextNode.outerHtml();
        //获取正文内的<a>标签
        Elements backLinks = contextNode.select("a");
        //取得最后一个
        Element backLink = backLinks.last();
        if (backLink !=null && backLink.text().equals("返回")){
            outerHtml = outerHtml.replace(backLink.outerHtml(),"");
        }
        titleItem.put("context", outerHtml);
    }


}
